package cn.tm.ms.memory.handler.support.bean;

import java.sql.ResultSet;
import java.sql.SQLException;

import cn.tm.ms.memory.handler.ResultSetHandler;
import cn.tm.ms.memory.support.BeanProcessor;

/**
 * 执行结果为Bean对象的拦截器
 * @author lry
 * @param <T>
 */
public class BeanHandler<T> implements ResultSetHandler<T> {
	
	private final Class<T> type;
	private BeanProcessor convert;
	
	public BeanHandler(Class<T> type) {
		this.type = type;
		this.convert = new BeanProcessor();
	}

	public T handle(ResultSet rs)  {
		 try {
			return rs.next() ? this.convert.toBean(rs, this.type) : null;
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
}
